/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package fr.castorflex.ohos.verticalviewpager.transformer;

import fr.castorflex.ohos.verticalviewpager.VerticalViewPager;
import ohos.agp.components.Component;

/**
 * Base transformer
 *
 * @author stt
 * @since 2021-04-26
 */
public abstract class BaseTransformer implements VerticalViewPager.PageTransformer {
    private static final float SCROLL_RIGHT = -1.0F;
    private static final float SCROLL_LEFT = 1.0F;

    @Override
    public void transformPage(Component component, float position) {
        float clampedPosition = clampPosition(position);
        onPreTransform(component, clampedPosition);
        onTransform(component, clampedPosition);
        onPostTransform(component, clampedPosition);
    }

    /**
     * PreTransform
     *
     * @param component component
     * @param position position
     */
    protected void onPreTransform(Component component, float position) {
        float width = (float) component.getWidth();
        component.setRotation(0.0F);
        component.setScaleX(1.0F);
        component.setScaleY(1.0F);
        component.setPivotX(0.0F);
        component.setPivotY(0.0F);
        component.setTranslationY(0.0F);
        component.setTranslationX(isPagingEnabled() ? 0.0F : -width * position);
        if (hideOffscreenPages()) {
            if (position <= SCROLL_RIGHT || position >= SCROLL_LEFT) {
                component.setAlpha(0f);
            } else {
                component.setAlpha(1f);
            }
            component.setEnabled(false);
        } else {
            component.setEnabled(true);
            component.setAlpha(1.0F);
        }
    }

    private float clampPosition(float position) {
        float clampPosition;
        if (position <= SCROLL_RIGHT) {
            clampPosition = SCROLL_RIGHT;
        } else if (position >= 1F) {
            clampPosition = 1.0F;
        } else if (Float.isNaN(position)) {
            clampPosition = 0.0F;
        } else {
            clampPosition = position;
        }
        return clampPosition;
    }

    /**
     * Is paging enabled
     *
     * @return false
     */
    public boolean isPagingEnabled() {
        return false;
    }

    /**
     * Hide Off screen Pages
     *
     * @return true
     */
    protected boolean hideOffscreenPages() {
        return true;
    }

    /**
     * PostTransform
     *
     * @param component component
     * @param position position
     */
    protected void onPostTransform(Component component, float position) {
    }

    /**
     * Abstract transform
     *
     * @param component component
     * @param position position
     */
    protected abstract void onTransform(Component component, float position);
}
